using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using ARF.Web.Interfaces;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using Mvp.Xml.Common;
using Microsoft.SharePoint;
using System.ComponentModel;
using System.Web.UI;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:Menu runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class Menu : System.Web.UI.WebControls.Menu, IXmlProvider
    {
        IXPathNavigable _xmlDocument;
        XslRenderControl _xslRender;

        public XslRenderControl Xsl
        {
            get 
            {
                if (_xslRender == null) _xslRender = `;
                return _xslRender; 
            }

            set { _xslRender = value; }
        }

        #region IXmlProvider Members

        public IXPathNavigable XmlAsObject
        {
            get
            {
                EnsureXml();
                return _xmlDocument; 
            }
            set
            {
                _xmlDocument = value;
            }
        }

        public string XmlAsString
        {
            get
            {
                if (_xmlDocument is XmlDocument)
                    return ((XmlDocument)_xmlDocument).OuterXml;

                return "";
            }
            set
            {
                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml(value);
                _xmlDocument = oDoc;
            }
        }

        #endregion

        protected void EnsureXml()
        {
            if (_xmlDocument == null) BuildXml();
        }

        protected virtual void BuildXml()
        {
            try
            {
                StringWriter sw = new StringWriter();
                XmlTextWriter w = new XmlTextWriter(sw);

                Uri currentPath = new Uri(SPContext.Current.Web.Url.ToLower());
                string sCurrentPath = currentPath.AbsolutePath.ToLower();

                w.WriteStartElement("Items");
                WriteNavigationNodes(this.Items, w, sCurrentPath);

                w.Close();
                sw.Close();

                XmlAsString = sw.ToString();
            }
            catch (Exception ex)
            {
                Log.Trace(ex);
            }
        }

        protected virtual void WriteNavigationNodes(MenuItemCollection menuItemCollection, XmlTextWriter w, string sCurrentPath)
        {
            foreach (MenuItem oItem in menuItemCollection)
            {
                w.WriteStartElement("Item");
                
                w.WriteAttributeString("Url", oItem.NavigateUrl);
                w.WriteAttributeString("Title", oItem.Text);
                w.WriteAttributeString("Value", oItem.Value);
                w.WriteAttributeString("ValuePath", oItem.ValuePath);
                w.WriteAttributeString("ToolTip", oItem.ToolTip);
                w.WriteAttributeString("Target", oItem.Target);
                w.WriteAttributeString("Selected", oItem.Selected.ToString());
                w.WriteAttributeString("ImageUrl", oItem.ImageUrl);
                w.WriteAttributeString("Depth", oItem.Depth.ToString());
                w.WriteAttributeString("Enabled", oItem.Enabled.ToString());
                w.WriteAttributeString("Selectable", oItem.Selectable.ToString());
                w.WriteAttributeString("Current", (sCurrentPath+"/").StartsWith(oItem.DataPath + "/", StringComparison.OrdinalIgnoreCase).ToString());

                WriteNavigationNodes(oItem.ChildItems, w, sCurrentPath);

                w.WriteEndElement();
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!Xsl.IsCached)
                Xsl.XmlObject = XmlAsObject;

            Xsl.RenderControl(writer);
        }
    }
}
